package com.ys.web.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ys.web.pojo.InverseKeywordInfo;
import com.ys.web.pojo.MainBussinessInfo;
import com.ys.web.pojo.MaterialCreateInfo;
import com.ys.web.service.CreateMaterielService;
import com.ys.web.service.ExportBaiduCSVService;
import com.ys.web.service.MainBussinessService;
import com.ys.web.service.MaterialCreateService;

/**
 * 物料生成
 * @author wdj
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/materialController")
public class MaterialController {
	@Autowired
	private MaterialCreateService materialCreateService;
	
	@Autowired
	private CreateMaterielService createMaterielService;
	
	@Autowired
	private ExportBaiduCSVService exportBaiduCSVService;
	
	@Autowired
	private MainBussinessService mainBussinessService;
	/**
	 * 显示物料生成
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping(params = "showMaterial")
	public ModelAndView showMaterial(HttpServletRequest httpServletRequest) {
		return new ModelAndView("material/materialInfo");
	}
	
	/**
	 * 获取生成的物料信息列表
	 * @return
	 */
	@RequestMapping(params = "listMaterial")
	@ResponseBody
	public List<MaterialCreateInfo> listMaterial(HttpServletRequest request) {
		String industryId = request.getParameter("industryId"); 
		String mainBussiness = request.getParameter("promotionPlanName");
//		try {
//			mainBussiness = new String(mainBussiness.getBytes("8859_1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		if(mainBussiness != null && mainBussiness.length() > 0) {
			//return materialCreateService.findMaterialCreateInfo(industryId, mainBussiness, 1);
			return createMaterielService.findMaterialCreateInfo(industryId, mainBussiness, 1);
			
		} else {
			return null;
		}
	}
		
	/**
	 * 测试数据
	 * @return
	 */
	@Deprecated
	private List<MaterialCreateInfo>  setMateriaListInfo2() {
		List<MaterialCreateInfo> list = new ArrayList<>();
		MaterialCreateInfo plan = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "生活服务", "搬家");
		list.add(plan);
		
		MaterialCreateInfo unit1 = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "核心词", "有效", "启用", "1", "未设置", "0.1", "计算机设备优先");
		list.add(unit1);
		MaterialCreateInfo unit2 = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "拓展词", "有效", "启用", "1", "未设置", "0.1", "计算机设备优先");
		list.add(unit2);
		MaterialCreateInfo unit3 = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "高频词", "有效", "启用", "1", "未设置", "0.1", "计算机设备优先");
		list.add(unit3);
		
		MaterialCreateInfo keyword = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "-", "-", "-", "-", "-", "-", "-",
				"医用塑胶地板厂家", "启用", "关键词计算机质量度", "关键词移动质量度", "关键词匹配模式",8.88, 1.0);
		list.add(keyword);
		MaterialCreateInfo keyword2 = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "-", "-", "-", "-", "-", "-", "-",
				"pvc 塑胶地板", "启用", "关键词计算机质量度", "关键词移动质量度", "关键词匹配模式",2.33, 2.0);
		list.add(keyword2);
		
		MaterialCreateInfo originality = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "-", "-", "-", "-", "-", "-", "-",
				"{医用塑胶地板厂家},选广州川钰!品牌厂家,免费寄送样品!", "创意描述1", "创意描述2", "创意默认访问URL", "创意默认显示URL", "创意移动访问URL", "创意移动显示URL", "创意状态", "创意启用/暂停信息", "创意设备偏好", "","","","","");
		list.add(originality);
		
		MaterialCreateInfo subchain = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "-", "-", "-", "-", "-", "-", "-",
				"", "", "", "", "", "", "", "", "", "", "", "", "");
		list.add(subchain);
		
		MaterialCreateInfo phone = new MaterialCreateInfo("402894b85bed5e8e015bed64d4000003", "-", "-", "-", "-", "-", "-", "-", "-", "-", 
				"", "", "");
		list.add(phone);
		
		return list;
	}
	
	
	
	@RequestMapping(params = "exportCSV")
	@ResponseBody
	public void exportCSV(HttpServletRequest req,HttpServletResponse resp){
		String industryId = req.getParameter("industryId");
		String mainBussiness = req.getParameter("mainBussiness");
		String result = exportBaiduCSVService.exporMetrialCreateCsv(industryId, mainBussiness, 1);
		
		try {
			resp.setContentType("application/csv;charset=UTF-8");
			resp.setHeader("Content-Disposition",
		      "attachment; filename=test.csv");
			OutputStream out = resp.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(resp.getOutputStream(), "UTF-8");
			osw.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF })); //导出csv文件需添加bom头信息  
			osw.write(result);
			osw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	@RequestMapping(params = "manageMainBussiness")
	public ModelAndView manageMainBussiness(HttpServletRequest httpServletRequest) {
		return new ModelAndView("material/manageMainBussiness");
	}
	/**
	 * 添加主营业务
	 */
	@RequestMapping(params = "addMainBussiness")
	@ResponseBody
	public String addMainBussiness(HttpServletRequest req,HttpServletResponse resp){
		String industryId = req.getParameter("industryId");
		String industryName = req.getParameter("industryName");
		String mainBussiness = req.getParameter("mainBussiness");
//		try {
//			industryName = new String(industryName.getBytes("8859_1"),"utf-8");
//			mainBussiness = new String(mainBussiness.getBytes("8859_1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return mainBussinessService.addMainBussiness(industryId, industryName, mainBussiness);
	}
	
	@RequestMapping(params = "getMainBussinessByIndustryId")
	@ResponseBody
	public List<MainBussinessInfo> getMainBussinessByIndustryID(HttpServletRequest req,HttpServletResponse resp){
		String industryId = req.getParameter("industryId");
		
		return mainBussinessService.getMainBussinessByIndustryId(industryId);
		
	}
	
	@RequestMapping(params = "addInverse")
	@ResponseBody
	public String addInverse(HttpServletRequest req,HttpServletResponse resp){
		String industryId = req.getParameter("industryId");
		String industryName = req.getParameter("industryName");
		int mid = Integer.valueOf(req.getParameter("mid"));
		String mainBussiness = req.getParameter("mainBussiness");
		String inverseKeyword = req.getParameter("inverseKeyword");
		String matchedPattern = req.getParameter("matchedPattern");
		
//		try {
//			industryName = new String(industryName.getBytes("8859_1"),"utf-8");
//			mainBussiness = new String(mainBussiness.getBytes("8859_1"),"utf-8");
//			inverseKeyword = new String(inverseKeyword.getBytes("8859_1"),"utf-8");
//			matchedPattern = new String(matchedPattern.getBytes("8859_1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
		return mainBussinessService.addInverse(industryId, industryName,mid, mainBussiness, inverseKeyword, matchedPattern);
		
	}
	@RequestMapping(params = "showInverse")
	public ModelAndView showInverse(HttpServletRequest httpServletRequest) {
		ModelAndView mav = new ModelAndView("material/inverseList");
		String industryId = httpServletRequest.getParameter("industryId");
		String mainBussiness = httpServletRequest.getParameter("mainBussiness");
//		try {
//			mainBussiness = new String(mainBussiness.getBytes("8859_1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		mav.addObject("industryId", industryId);
		mav.addObject("mainBussiness", mainBussiness);
		return mav;
	}
	@RequestMapping(params = "getInverseList")
	@ResponseBody
	public List<InverseKeywordInfo> getInverseList(HttpServletRequest req,HttpServletResponse resp){
		
		String industryId = req.getParameter("industryId");
		String mainBussiness = req.getParameter("mainBussiness");
//		try {
//			mainBussiness = new String(mainBussiness.getBytes("8859_1"),"utf-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		return mainBussinessService.getInverse(industryId, mainBussiness);
		
	}
	
}
